<template>
  <div class="profile-wrapper">
    <!-- 顶部tarBar -->
    <van-nav-bar
      title="个人信息"
      left-arrow
      fixed
      @click-left="$router.go(-1)"
      class="animate__fadeInDown animate__animated"
    />
    <!-- 顶部tarBar -->
    <div class="username-wrapper" :class="{ fade: show }">
      <van-cell-group inset>
        <van-cell title="头像" clickable>
          <template #default>
            <div class="img-wrapper">
              <img src="../../../assets/image/icon.png" alt="" />
            </div>
          </template>
        </van-cell>
        <van-cell title="账号" :value="$store.state.username"></van-cell>
      </van-cell-group>
    </div>
    <div class="nickname-wrapper" :class="{ fade: show }">
      <van-cell-group inset>
        <van-cell
          title="昵称"
          :value="nickname"
          is-link
          @click="show = true"
        ></van-cell>
        <van-cell
          title="性别"
          is-link
          :value="gender"
          @click="genderShow = true"
        ></van-cell>
        <van-cell
          title="生日"
          is-link
          :value="birthday"
          @click="birthdayShow = true"
        ></van-cell>
      </van-cell-group>
    </div>
    <van-button :class="{ fade: show }" class="notLogin-btn" @click="notLogin"
      >退出登录</van-button
    >

    <!-- 弹出框 -->
    <transition
      enter-active-class="animate__animated animate__rotateIn"
      leave-active-class="animate__animated animate__zoomOutUp"
    >
      <div class="nickname-popup" v-if="show">
        <div class="wrapper">
          <div class="title">请输入您的昵称</div>
          <input type="text" autofocus v-model="inputNickname" />
          <div class="btn">
            <button class="cancel" @click="cancel">取消</button>
            <button class="confirm" @click="confirm">确认</button>
          </div>
        </div>
      </div>
    </transition>

    <!-- 选择性别弹出层 -->
    <van-popup
      v-model="genderShow"
      position="bottom"
      :style="{ height: '48%' }"
    >
      <van-picker
        title="性别"
        show-toolbar
        :columns="columns"
        @confirm="onConfirm"
        @cancel="genderShow = false"
      />
    </van-popup>
    <!-- 选择性别弹出层 -->
    <!-- 选择生日弹出层 -->
    <van-popup
      v-model="birthdayShow"
      position="bottom"
      :style="{ height: '48%' }"
    >
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择生日"
        @confirm="birthdayConfirm"
        @cancel="birthdayShow=false"
      />
    </van-popup>
    <!-- 选择生日弹出层 -->
  </div>
</template>

<script>
import moment from "moment"
export default {
  name: "Profile",
  data() {
    return {
      // 点击昵称单元格是否显示
      show: false,
      nickname: "",
      birthday: "",
      gender: "",
      // 输入的昵称
      inputNickname: "",
      genderShow: false,
      birthdayShow: false,
      columns: ["男", "女"],
      currentDate: new Date()
    };
  },
  methods: {
    // 点击退出登录
    notLogin() {
      this.$store.commit("changeToken", null);
      this.$router.push("/home");
    },
    // 输入昵称弹出框的取消
    cancel() {
      this.show = false;
    },
    // 输入昵称弹出框的确定
    confirm() {
      this.nickname = this.inputNickname;
      this.$store.commit("setNickname",this.nickname)
      this.show = false;
    },
    // 确定性别
    onConfirm(val) {
      this.gender = val;
      this.$store.commit("setGender",this.gender)
      this.genderShow = false;
    },
    // 确定生日 
    birthdayConfirm (val) {
      this.birthday = moment(val).format('YYYY-MM-DD')
      this.$store.commit("setBirthday",this.birthday)
      this.birthdayShow = false
    }
  },
};
</script>

<style lang="scss" scoped>
.fade {
  opacity: 0.3;
}
.profile-wrapper {
  width: 100%;
  height: 100vh;
  background: rgb(241, 240, 240);
  position: relative;
  .username-wrapper {
    padding-top: 100px;
    .img-wrapper {
      border-radius: 15px;
      img {
        width: 30px;
        height: 30px;
      }
    }
  }
  .nickname-wrapper {
    margin-top: 15px;
  }
  .notLogin-btn {
    width: 343px;
    height: 46px;
    margin: 30px 15px;
    border-radius: 10px;
    background-color: #ff3f34;
    color: white;
    font-size: 16px;
  }
  .nickname-popup {
    width: 200px;
    padding: 10px;
    background-color: rgb(255, 255, 255);
    border-radius: 8px;
    position: absolute;
    top: 40%;
    left: 77.5px;
    .wrapper {
      .title {
        height: 20px;
        color: #333;
        text-align: center;
        line-height: 20px;
        margin-bottom: 5px;
        font-size: 15px;
      }
      input {
        box-shadow: unset;
        border: 1px solid #d2dae2;
        height: 23px;
        margin-bottom: 10px;
        padding: 3px;
        border-radius: 3px;
        color: rgb(24, 23, 23);
        font-size: 14px;
        padding-left: 15px;
      }
      .btn {
        display: flex;
        justify-content: space-between;
        align-items: center;
        button {
          border: unset;
          width: 47%;
          height: 40px;
          background: pink;
          border-radius: 5px;
        }
        .cancel {
          background-color: #55e6c1;
        }
        .confirm {
          background-color: #fd7272;
        }
      }
    }
  }
}
</style>